﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gamego
{
    /// <summary>
    /// Bar类
    /// </summary>
    class Bar
    {
        //定义画图大小
        public Rectangle Rectangle { get; set; }
        //定义物体移动的速度
        private const int step = 40;
        private const int BarWidth = 30;
        private int _gameWidth;


        public Bar(int gameWidth)
        {
            _gameWidth = gameWidth; 
        
            Rectangle = new Rectangle((_gameWidth-BarWidth )/2, 410, BarWidth, 25);
        }
        /// <summary>
        /// 绘制Bar
        /// </summary>
        /// <param name="g"></param>
        public void Draw(Graphics g)
        {

            g.FillRectangle(new SolidBrush(Color.Black ), Rectangle);

        }
        /// <summary>
        /// 处理键盘
        /// </summary>
        /// <param name="key"></param>
        public void KeyDown(String key)
        {
            int newX = Rectangle.X;
            if (key == "A")
            {
                newX -= step;
                //防止左边溢出
                if (newX < 0)
                    newX = 0;


            }
               
            
            if (key == "D")
                
            {
                newX += step;
                if (newX + BarWidth > _gameWidth)
                    newX = _gameWidth - BarWidth;

            }
            

                Rectangle = new Rectangle(newX,
                                          Rectangle.Y,
                                          Rectangle.Width,
                                          Rectangle.Height);



        }

       
        }

    }

        
        
